#include "GTAMoveBoard.h"
#include "GTACommon.h"

#include "TileInfo.h"
#include "NextTileInfo.h"
#include "NextTileSet.h"
#include "TileMgr.h"

GTAMoveBoard::GTAMoveBoard(void)
	: mMoveBlockSprite(NULL)
	, mNextBlockSprite(NULL)
	, tileMgr(NULL)
{
	memset( mMoveBoard, 0, sizeof( mMoveBoard ) );
	memset( mNextBoard, 0, sizeof( mNextBoard ) );
}

GTAMoveBoard::~GTAMoveBoard(void)
{
	_ShutDown();
}

bool GTAMoveBoard::Init(void)
{
	if( ! _InitMoveBlockSprite() )
		return false;

	if( ! _InitNextBlockSprite() )
		return false;

	if( ! _InitTileMgr() )
		return false;

	return true;
}

void GTAMoveBoard::Update(float delta)
{
	tileMgr->Update();

	memset( mMoveBoard, 0, sizeof( mMoveBoard ) );

	int posX, posY;
	for( int i = 0; i < tileNum; i++ )
	{
		posX = tileMgr->move_tile_set->next_tile[i]->widthPosition;
		posY = tileMgr->move_tile_set->next_tile[i]->heightPosition;

		if( (posX < 0 || 20 < posX) || (posY < 0 || 20 < posY) )
		{
			memset( mMoveBoard, 0, sizeof( mMoveBoard ) );
			tileMgr->crash = true;
			break;
		}

		mMoveBoard[posY][posX] = MOVE_BLOCK;
	}

	posX = 0, posY = 0;
	memset( mNextBoard, 0, sizeof( mNextBoard ) );

	for( int i = 0; i < tileSetNum; i++ )
	{
		for( int j = 0; j < tileNum; j++ )
		{
			tileMgr->next_tile_set[i]->Get_tileSet_in_position(&posX, &posY, j);
			mNextBoard[i][posY][posX] = NEXT_BLOCK;
		}
	}
}

void GTAMoveBoard::Render(void)
{
	for(int i = 0; i < MOVE_BOARD_HEIGHT; i++)
	{
		for(int z = 0; z < MOVE_BOARD_WIDTH; z++)
		{
			if( EMPTYSPACE == mMoveBoard[i][z] )
				continue;

			if( MOVE_BLOCK == mMoveBoard[i][z] )
				mMoveBlockSprite->Render( z * MOVE_BLOCK_SIZE_X + MOVE_BOARD_START_X,
				i * MOVE_BLOCK_SIZE_Y + MOVE_BOARD_START_Y );
		}
	}

	for(int i = 0; i < tileSetNum; i++)
	{
		int start_x = 0, start_y = 0;
		switch(i)
		{
		case 0:
			start_x = NORTH_START_X, start_y = NORTH_START_Y;
			break;
		case 1:
			start_x = WEST_START_X, start_y = WEST_START_Y;
			break;
		case 2:
			start_x = SOUTH_START_X, start_y = SOUTH_START_Y;
			break;
		case 3:
			start_x = EAST_START_X, start_y = EAST_START_Y;
			break;
		} 

		for(int j = 0; j < tileNum; j++)
		{
			for(int k = 0; k < tileNum; k++)
			{
				if( NEXT_BLOCK == mNextBoard[i][j][k] )
					mNextBlockSprite->Render( k * NEXT_BLOCK_SIZE_X + start_x, j * NEXT_BLOCK_SIZE_Y + start_y );
			}
		}
	}
}

bool GTAMoveBoard::_InitMoveBlockSprite(void)
{
	if( NULL == mMoveBlockSprite )
	{
		mMoveBlockSprite = new GTASprite;

		GTATexture* pTexture = GTATextureManager::Inst().LoadTexture( L"DATA/IMAGE/MapObj/BlockTile.png" );
		if( NULL == pTexture )
		{
			GTACommon::DebugMessage( L"InitGameBoard Failed.\n" );
			return false;
		}

		mMoveBlockSprite->SetSprite( pTexture );
		mMoveBlockSprite->SetSize( pTexture->GetWidth(), pTexture->GetHeight() );
		mMoveBlockSprite->SetPos( 0, 0 );

		return true;
	}

	return false;
}

bool GTAMoveBoard::_InitNextBlockSprite(void)
{
	if( NULL == mNextBlockSprite )
	{
		mNextBlockSprite = new GTASprite;

		GTATexture* pTexture = GTATextureManager::Inst().LoadTexture( L"DATA/IMAGE/MapObj/BlockTile.png" );
		if( NULL == pTexture )
		{
			GTACommon::DebugMessage( L"InitGameBoard Failed.\n" );
			return false;
		}

		mNextBlockSprite->SetSprite( pTexture );
		mNextBlockSprite->SetSize( pTexture->GetWidth() / 2, pTexture->GetHeight() / 2 );
		mNextBlockSprite->SetPos( 0, 0 );

		return true;
	}

	return false;
}

bool GTAMoveBoard::_InitTileMgr(void)
{
	if( NULL == tileMgr )
	{
		tileMgr = new TileMgr;

		int posX, posY;
		for( int i = 0; i < tileNum; i++ )
		{
			posX = tileMgr->move_tile_set->next_tile[i]->widthPosition;
			posY = tileMgr->move_tile_set->next_tile[i]->heightPosition;
			mMoveBoard[posY][posX] = MOVE_BLOCK;
		}

		posX = 0, posY = 0;
		for( int i = 0; i < tileSetNum; i++ )
		{
			for( int j = 0; j < tileNum; j++ )
			{
				tileMgr->next_tile_set[i]->Get_tileSet_in_position(&posX, &posY, j);
				mNextBoard[i][posY][posX] = NEXT_BLOCK;
			}
		}

		return true;
	}

	return false;
}

void GTAMoveBoard::_ShutDown(void)
{
	if( NULL != mMoveBlockSprite )
	{
		delete mMoveBlockSprite;
		mMoveBlockSprite = NULL;
	}

	if( NULL != mNextBlockSprite )
	{
		delete mNextBlockSprite;
		mNextBlockSprite = NULL;
	}

	if( NULL != tileMgr )
	{
		delete tileMgr;
		tileMgr = NULL;
	}
}